﻿function ajax(method, param, success) {
    $.ajax({
        url: "../System_Agency/AgencyList.aspx?m=" + method + "&rnd=" + Math.random()
        , dataType: "json"
        , type: "post"
        , data: param
        , success: function (data) {
            
            if (!AjaxError(data)) {
                success(data);
            }
        }
        , error: function (r) {
            alert(r.responseText);
        }
    });
};

function InitList() {
    BindCity();
    Search();
}

// 绑定城区列表
function BindCity() {
    $.ajax({ url: "../system_position/Proxy.ashx", type: "post", dataType: "json", async: false,
        data: { method: "listCity" }
        , success: function (json) {
            $("#ddl_City").empty().append('<option  value="-1">全部</option>').append("#full_City", json.listCity);
            CitySelected();
        }
    });
}

// 城区列表选择时绑定地区列表
function CitySelected() {
    var cityId = $("#ddl_City").val();   
    $("#ddl_Area").empty().append('<option  value="-1">全部</option>'); 
    $.ajax({ url: "../system_position/Proxy.ashx", type: "post", dataType: "json",
        async: false,
        data: { method: "listAreaByCity", cityId: cityId }
        , success: function (json) {
            
            $("#ddl_Area").append("#full_Area", json.listAreaByCity);
        }        
    });
}

function Search() {
    ajax("SearchAgency", { cid: $("#ddl_City").val(), rid: $("#ddl_Area").val(), name: $("#txtName").val() }, function (obj) {
        $("#tb_list").empty().append("#tmpl_head", {}).append("#tmpl_list",obj.data).treeTable({ expandable: true, initialState: "expanded" });
    });
}

function GetListItemBg() {
    GetListItemBg.index++;
    if (GetListItemBg.index % 2 == 0) {
        return "#ccc";
    }
    return "#fff";
}

GetListItemBg.index = 1;

function AddFirstAgency() {
    var brid1 = $("#tb_list tr[pid=-1]:last").attr("id").replace("n", "");
    window.location.replace("AgencyAdd.aspx?pid=-1&bid1=" + brid1 + "&bid2=-1");
}

function InitAdd() {
    $("#pid").val(QueryString("pid"));
    $("#bid1").val(QueryString("bid1"));
    $("#bid2").val(QueryString("bid2"));
    
    $("#ddl_City").change(function () {       
        $("#ddl_Area :first-child").remove();
    });

    BindCity();

    $("#ddl_Area :first-child").remove();
}

function SaveAdd() {
    ajax("AddAgency", {
        pid: $("#pid").val()
        , bid1: $("#bid1").val()
        , bid2: $("#bid2").val()
        , cid: $("#ddl_City").val()
        , rid: $("#ddl_Area").val()
        , code: $("#code").val()
        , name: $("#name").val()
        , hasAutority: ($("#hasAutority").is(":checked") ? 1 : 0)
    }, function (obj) {
        
        if (obj.success - 0 == 1) {
            alert("添加成功");
            window.location.replace("AgencyList.aspx");
        }
        else {
            alert("添加失败");
        }
    });
}

// 添加同级
function AddAgency(id, pid) {
    var bid1 = id;
    var bid2 = $("#tb_list tr[pid=" + pid + "]");
    var n = bid2.size();
    $.each(bid2, function (i, v) {
        if ($(v).attr("myid") - id == 0) {
            if (i == n - 1) {
                bid2 = -1;
            }
            else {
                bid2 = $(bid2.get(i + 1)).attr("myid") - 0;
            }
            return false;
        }
    });

    window.location.replace("AgencyAdd.aspx?pid="+pid+"&bid1=" + bid1 + "&bid2="+bid2);
}

function AddSubAgency(id, pid) {
    var bid2 = -1;
    var bid1 = $("#tb_list tr[pid=" + id + "]:last").attr("myid");
    if (!bid1) {
        bid1 = -1;
    }
    window.location.replace("AgencyAdd.aspx?pid=" + id + "&bid1=" + bid1 + "&bid2=" + bid2);
}

function Edit(id) {
    window.location.replace("AgencyEdit.aspx?id=" + id );
}

function InitEdit() {
    var id = QueryString("id");
    $("#id").val(id);
    ajax("GetAgency", { id: id }, function (obj) {
        $("#code").val(obj.code);
        $("#name").val(obj.name);
        var b = (obj.hasAutority - 0) == 1; 
        if (b) {
            $("#hasAutority").attr("checked", "checked");
        }
    });
}

function SaveEdit() {
    ajax("ModifyAgency", {
        id: $("#id").val()       
        , code: $("#code").val()
        , name: $("#name").val()
        , hasAutority: ($("#hasAutority").is(":checked") ? 1 : 0)
    }, function (obj) {

        if (obj.success - 0 == 1) {
            alert("修改成功");
            window.location.replace("AgencyList.aspx");
        }
        else {
            alert("修改失败");
        }
    });
}

function Delete(id) {
    if (!confirm("确认删除")) {
        return;
    }
    ajax("DeleteAgency", {
        id: $("#id").val()
    }, function (obj) {

        if (obj.success - 0 == 1) {
            alert("删除成功");
            Search();
        }
        else {
            alert("删除失败");
        }
    });
}

function Up(id, pid) {
    var arr = $("#tb_list tr[pid=" + pid + "]");
    if ($(arr[0]).attr("myid") - id == 0) {
        alert("不能移动了");
        return;
    }
    var id1 = id;
    var id2;
    $.each(arr, function (i, v) {
        if ($(v).attr("myid") - id == 0) {
            id2 = $(arr[i - 1]).attr("myid") - 0;
            return false;
        }
    });
    ajax("ExchangeAgencyOrder", {
        id1: id1, id2: id2
    }, function (obj) {
        if (obj.success - 0 == 1) {
            alert("移动成功");
            Search();
        }
        else {
            alert("移动失败");
        }
    });
}

function Down(id, pid) {
    var arr = $("#tb_list tr[pid=" + pid + "]");
    if ($(arr).find(":last").attr("myid") - id == 0) {
        alert("不能移动了");
        return;
    }
    var id1 = id;
    var id2;
    $.each(arr, function (i, v) {
        if ($(v).attr("myid") - id == 0) {
            id2 = $(arr[i + 1]).attr("myid") - 0;
            return false;
        }
    });
    ajax("ExchangeAgencyOrder", {
        id1: id1, id2: id2
    }, function (obj) {
        if (obj.success - 0 == 1) {
            alert("移动成功");
            Search();
        }
        else {
            alert("移动失败");
        }
    });
}

function SetAuthority(id, pid, name, code) {
    window.location.replace("../System_Manager/AuthorityAgency.aspx?type=1&id=" + id);
}